D:\a\tools.proto\tools.proto\compiler\src\compiler\imports.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::message::Message; |
30 | | use crate::compiler::r#enum::Enum; |
31 | | use crate::compiler::structure::Structure; |
32 | | use crate::compiler::union::Union; |
33 | | use crate::compiler::util::types::{Name, PtrKey}; |
34 | | use crate::compiler::Protocol; |
35 | | use crate::model::typedef::Typedef; |
36 | | use std::rc::Rc; |
37 | | |
38 | | #[derive(Copy, Clone)] |
39 | | pub enum Import<'a> { |
40 | | Struct(&'a Rc<Structure>), |
41 | | Enum(&'a Rc<Enum>), |
42 | | Union(&'a Rc<Union>), |
43 | | Message(&'a Rc<Message>), |
44 | | Type(&'a Rc<Typedef>), |
45 | | } |
46 | | |
47 | | impl Name for Import<'_> { |
48 | 198 | fn name(&self) -> &str { |
49 | 198 | match self { |
50 | 158 | Import::Struct(v) => v.name(), |
51 | 4 | Import::Enum(v) => v.name(), |
52 | 2 | Import::Union(v) => v.name(), |
53 | 30 | Import::Message(v) => v.name(), |
54 | 4 | Import::Type(v) => v.name(), |
55 | | } |
56 | 198 | } |
57 | | } |
58 | | |
59 | | impl PtrKey for Import<'_> { |
60 | 38 | fn ptr_key(&self) -> usize { |
61 | 38 | match self { |
62 | 26 | Import::Struct(v) => v.ptr_key(), |
63 | 2 | Import::Enum(v) => v.ptr_key(), |
64 | 2 | Import::Union(v) => v.ptr_key(), |
65 | 6 | Import::Message(v) => v.ptr_key(), |
66 | 2 | Import::Type(v) => v.ptr_key(), |
67 | | } |
68 | 38 | } |
69 | | } |
70 | | |
71 | | impl Import<'_> { |
72 | 38 | pub fn insert(self, type_name: String, proto: &mut Protocol) { |
73 | 38 | match self { |
74 | 26 | Import::Struct(v) => { |
75 | 26 | proto.structs.insert_import(type_name, v.clone()); |
76 | 26 | } |
77 | 2 | Import::Enum(v) => { |
78 | 2 | proto.enums.insert_import(type_name, v.clone()); |
79 | 2 | } |
80 | 2 | Import::Union(v) => { |
81 | 2 | proto.unions.insert_import(type_name, v.clone()); |
82 | 2 | } |
83 | 6 | Import::Message(v) => { |
84 | 6 | proto.messages.insert_import(type_name, v.clone()); |
85 | 6 | } |
86 | 2 | Import::Type(v) => { |
87 | 2 | proto.types.insert_import(type_name, v.clone()); |
88 | 2 | } |
89 | | } |
90 | 38 | } |
91 | | } |